import Server from "@/config/server";
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode : false,
  dataLists: [],
  Fields: [],
  itemDetail: {},
  permissAll: [],
  roleAllData: [],
};
const actions = {
  //列表
  roleLists({commit}, page) {
    request.get(Server.action.role_list, { params: page })
    .then(response => {
      console.log(response);
        if (response.data.errorCode === 0) {
            commit("setLists", response.data.result.list);
            let fields = response.data.result.fields;
            fields.push(
            // {
            //     title: '权限',
            //     align: 'center',
            //     width: 80,
            //     scopedSlots: { customRender: 'root' },
            //     fixed: 'right',
            // },
            {
                title: '操作',
                align: 'center',
                width: 240,
                scopedSlots: { customRender: 'action' },
                fixed: 'right',
            })
            fields.forEach(function (item, i) {
              fields[i].dataIndex = fields[i].key
              fields[i].align = 'center';
              if(i < fields.length-2) {
                fields[i].width = item.width || "15%"
              }
            })
            commit("setFields", fields);
        } else {

        }
    })
  },
  //添加
  roleAdd({commit}, page) {
    commit("setErrorCode", false);
    request.post(Server.action.role_add,  page )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else if(response.status === 422) {
        showErrors( response.data.errors )
      } else {
        showError( response.data.message )
      }
    })
  },
  //编辑
  roleEdit({commit}, page) {
    commit("setErrorCode", false);
    request.post(Server.action.role_update, page )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  // 删除
  roleDel({commit}, id) {
    request.get(Server.action.role_delete + id)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  // 详情
  roleDetail({commit}, id) {
    request.get(Server.action.role_detail + id)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        commit("setDetail", response.data.result);
        // showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },

  //权限所有信息
  permissionAll({commit}) {
    request.get(Server.action.permission_all)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        // permissionAll
        let data = response.data.result
        if (typeof data != 'undefined') {
          data.forEach(items=>{
            items.children.forEach(item=>{
              // item.is_pay = false
              item.scopedSlots = { title: 'isPay'}
            })
          })
        }
        commit("setPermissionAll", data);
        // showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  //权限批量操作
  setPower({commit}, itemForm) {
    commit("setErrorCode", false);
    request.post(Server.action.rolePermission_batchOperate, itemForm )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  // 所有角色信息
  roleAll({commit}) {
    request.get(Server.action.role_all)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        commit("setroleAll", response.data.result);
      } else {
        // showError( response.data.message )
      }
    })
  },
};
const mutations = {
  setErrorCode(state, text) {
	    state.requestCode = text;
	},
	setLists(state, lists) {
	    state.dataLists = lists;
	},
  setFields(state, lists) {
	    state.Fields = lists;
	},
  setDetail(state, data) {
      state.itemDetail = data;
  },
  setPermissionAll(state, data) {
      state.permissAll = data;
  },
  setroleAll(state, data) {
    state.roleAllData = data;
},
};
export default {
    namespaced: true,
    state,
    actions,
    mutations
};
